package com.wisemoresystem.seal.action_app.auth;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.model.t_empower;
import com.wisemoresystem.seal.model.t_seal_auth;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.service.AuthService;
import com.wisemoresystem.seal.util.json.JsonTools;
import com.wisemoresystem.seal.web.WebTools;

@Controller("ixNetAuth")
@Scope("prototype")
@RequestMapping("/ix/auth/net")
public class AuthAction extends ProjBaseAction {

	@Autowired
	private AuthService authService;

	/**
	 * 转授权列表
	 * 
	 * @param srh_name
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/list")
	@ResponseBody
	public String list(String srh_name) throws IOException {

		List<String> ulist = authService.selectNotAuth();
		if (ChkTools.isNotNull(ulist)) {
			for (String user_id : ulist) {
				t_user user = oc.findObjById(user_id, t_user.class);
				user.setUser_level("非主管");
				oc.updateObj(user);
			}
		}
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(System.currentTimeMillis());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String today = df.format(c.getTime());
		List<Map<String, Object>> list = authService.selectSealAuthList(null,
				today, page);
		t_empower power = oc.findObjById("123", t_empower.class);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("num", power.getNum());
		list.add(map);

		return JsonTools.toJson(list);
	}

	/**
	 * 去向添加页面
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/add_form")
	@ResponseBody
	public String add_form() throws IOException {

		List<Map<String, Object>> userlist = authService.selectSealUserList();

		return JsonTools.toJson(userlist);
	}

	/**
	 * 添加转授权
	 * 
	 * @param auth
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/add")
	@ResponseBody
	public String add(t_seal_auth auth) throws IOException {

		if (ChkTools.isNull(auth.getTime_get())) {
			auth.setTime_get(new Timestamp(System.currentTimeMillis()));
		}
		auth.setBank_id(WebTools.getCurrentBankId());
		auth.setFlag("生效");
		auth.setUser_id(WebTools.getCurrentUserId());
		oc.saveObj(auth);

		t_user user = oc.findObjById(auth.getEnd_user_id(), t_user.class);
		user.setUser_level("三级主管");
		oc.updateObj(user);

		logT.logH("添加印章授权", t_seal_auth.class, auth.get_id());
		return "true";
	}

	/**
	 * 删除转授权
	 * 
	 * @param auth_id
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/del")
	@ResponseBody
	public String del(String auth_id) throws IOException {
		resp.setCharacterEncoding("utf-8");

		t_seal_auth auth = oc.findObjById(auth_id, t_seal_auth.class);
		auth.setFlag("失效");
		oc.updateObj(auth);
		t_user user = oc.findObjById(auth.getEnd_user_id(), t_user.class);
		user.setUser_level("非主管");
		oc.updateObj(user);

		logT.logH("印章授权失效", t_seal_auth.class, auth.get_id());
		return "true";

	}

}
